var myFolderList=null;
var precontext=null;
var my=null;

$(function () {
    myFolderList=$(".tablelist");
    precontext=$(".precontext");
    my=$(".my");

    getFolderAndFileList();


    my.click(function (){

        location.href="goMyFolderList";
    })

})


function getFolderAndFileList() {

    $.ajax({
        url: "/myFolder/folderAndFileList",
        data: {},
        dataType: "json",
        type: "post",
        success: function (result) {
            var str = "    \t<thead>\n" +
                "    \t<tr>\n" +
                "        <th>名称</th>\n" +
                "        <th>修改日期</th>\n" +
                "        <th>文件类型</th>\n" +
                "        <th>大小</th>\n" +
                "        <th style=\"width: 180px\">操作</th>\n" +
                "        </tr>\n" +
                "        </thead>\n" +
                "        \n" +
                "        <tbody>";

            //文件夹
            var result1 = result[0];
            if (result1 != null && result1.length!==0) {

                //判断是否为第二级目录，是则不需要
                $.ajax({
                    url: "/myFolder/goPreContextFirst",
                    data: {"folderId":result1[0].folderParentId},
                    dataType: "json",
                    type: "post",
                    success: function (result) {
                        if(result.folderParentId==="0"){
                            // location.href="goView";
                        }else {
                            var s="<a href=\"#\" class=\"query\" onclick='pre("+result1[0].folderParentId+")'>上一级目录</a>";
                            precontext.html(s);
                        }
                    }
                })

                for (let i = 0; i < result1.length; i++) {
                    str += "<tr>\n" +
                        "        <td style=\"height: 80px\"><img src='../../../static/images/folders.jpeg'>&nbsp;&nbsp;" + result1[i].folderName + "</td>\n" +
                        "        <td>" + result1[i].folderModifytime + "</td>\n" +
                        "        <td>文件夹 </td>\n" +
                        "        <td>" + result1[i].folderSize + "B</td>\n" +
                        "        <td>&nbsp;&nbsp;<a href=\"#\" class=\"query\" onclick='query("+result1[i].folderId+")'>查看</a> &nbsp;&nbsp;&nbsp;&nbsp;  <a href=\"deptUpdate.html\" class=\"add\" onclick='add("+result1[i].folderId+")'>新建</a> &nbsp;&nbsp;&nbsp;&nbsp;  <a href=\"deptUpdate.html\" class=\"modify\" onclick='modify("+result1[i].folderId+")'>修改</a> &nbsp;&nbsp;&nbsp;&nbsp;  <a href=\"#\" class=\"delete\" onclick='dete("+result1[i].folderId+")'>删除</a></td>\n" +
                        "        </tr>";
                }
            }


            //文件
            var result2 = result[1];
            if (result2 != null && result2.length!==0) {

                var s1="<a href=\"#\" class=\"query\" onclick='pre("+result2[0].folderId+")'>上一级目录</a>";
                precontext.html(s1);

                for (let i = 0; i < result2.length; i++) {
                    str += "<tr>\n" +
                        "        <td style=\"height: 80px\"><img src='../../../static/images/folders.jpeg'>&nbsp;&nbsp;" + result2[i].fileName + "</td>\n" +
                        "        <td>" + result2[i].fileModifytime + "</td>\n" +
                        "        <td>文件</td>\n" +
                        "        <td>" + result2[i].fileSize + "B</td>\n" +
                        // "        <td>&nbsp;&nbsp;<a href=\"#\" class=\"query\" onclick='query("+result2[i].folderId+")'>查看</a> &nbsp;&nbsp;&nbsp;&nbsp;  <a href=\"deptUpdate.html\" class=\"add\" onclick='add("+result2[i].folderId+")'>新建</a> &nbsp;&nbsp;&nbsp;&nbsp;  <a href=\"deptUpdate.html\" class=\"modify\" onclick='modify("+result2[i].folderId+")'>修改</a> &nbsp;&nbsp;&nbsp;&nbsp;  <a href=\"#\" class=\"delete\" onclick='delete("+result2[i].folderId+")'>删除</a></td>\n" +
                        "        </tr>";
                }
            }

            str += "        </tbody>";
            myFolderList.html(str);

        }
    })
}


function pre(folderId) {
    $.ajax({
        url: "/myFolder/goPreContext",
        data: {"folderId":folderId},
        dataType: "json",
        type: "post",
        success: function (result) {
            if(result){
                location.href="goView";
            }else {
                // location.href="../../../../templates/error.html";
            }
        }
    })
}



function query(folderId) {
    $.ajax({
        url: "/myFolder/goMyFolderView",
        data: {"folderId":folderId},
        dataType: "json",
        type: "post",
        success: function (result) {
            if(result){
                location.href="goView";
            }else {
                // location.href="../../../../templates/error.html";
            }
        }
    })
}



function add(folderId) {
    $.ajax({
        url: "/myFolder/goMyFolderAddView",
        data: {"folderId":folderId},
        dataType: "json",
        type: "post",
        success: function (result) {
            if(result){
                location.href="goAddView";
            }else {
                // location.href="../../../../templates/error.html";
            }
        }
    })
}


function modify(folderId) {
    $.ajax({
        url: "/myFolder/goMyFolderModifyView",
        data: {"folderId":folderId},
        dataType: "json",
        type: "post",
        success: function (result) {
            if(result){
                location.href="goModifyView";
            }else {
                // location.href="../../../../templates/error.html";
            }
        }
    })
}


function dete(folderId) {
    var b = confirm("您确定要删除吗？");
    if(b){
        $.ajax({
            url: "/myFolder/deleteMyFolder",
            data: {"folderId":folderId},
            dataType: "json",
            type: "post",
            success: function (result) {
                if(result){
                    alert("删除成功");
                    location.reload();
                }else {
                    // location.href="../../../../templates/error.html";
                }
            }
        })
    }

}